package org.example.dyscript.dynamicscript;

import org.apache.commons.jexl3.*;

import java.util.Map;

public class JexlRule implements Rule {

  private static final JexlEngine JEXL = new JexlBuilder().cache(1000).strict(true).create();

  JexlExpression executeExpression;
  JexlExpression evaluateExpression;

  String executeScript = "ali_score > 100 "
      + "&& wx_score < 450 "
      + "&& !pd_living_province.contains(\"乌克兰\") "
      + "&& pd_census_province.contains(\"吉尔吉斯斯坦\")";

  String evaluateScript = "age > 18 && vip.contains(\"vip\")";

  public JexlRule() {
    executeExpression = JEXL.createExpression(executeScript);
    evaluateExpression = JEXL.createExpression(evaluateScript);
  }

  @Override
  public boolean execute(Map<String, Object> parameters) {
    JexlContext jexlContext = new MapContext(parameters);
    return (boolean) executeExpression.evaluate(jexlContext);
  }

  @Override
  public boolean evaluate(Map<String, Object> parameters) {
    JexlContext jexlContext = new MapContext(parameters);
    return (boolean) evaluateExpression.evaluate(jexlContext);
  }
}
